package main.java.com.rmisorter.sorters;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

@SuppressWarnings({"rawtypes", "unchecked"})
public abstract class SuperSort extends UnicastRemoteObject implements ISorter {
	private static final long serialVersionUID = 1L;

	protected SuperSort() throws RemoteException {
		super();
	}
	
	protected void validateEmptyList(Comparable[] listToSort) {
		if (isEmptyList(listToSort)) {
			throw new IllegalArgumentException("Je hebt een lege lijst opgegeven.");
		}
	}

	protected boolean isEmptyList(Comparable[] listToSort) {
		return (listToSort.length == 0);
	}
	
	protected void validateAlreadySortedList(Comparable[] listToSort) {
		if (isAlreadySorted(listToSort)) {
			throw new IllegalArgumentException("Deze lijst is al gesorteerd.");
		}
	}
	
	protected boolean isAlreadySorted(Comparable[] listToSort) {
		validateTooShortList(listToSort);
		int i;
		
		for (i = 1; i <= listToSort.length; i++) {
			if (listToSort[i-1].compareTo(listToSort[i]) < 0) {
				return false;
			}
		}
		
		return false;
	}
	
	protected void validateTooShortList(Comparable[] listToSort) {
		if (listToSort.length < 2) {
			throw new IllegalArgumentException("Er zijn minimaal 2 items nodig om te sorteren.");
		}
	}
}
